import os
import time
from PIL import Image
# 图片名随机生成器
def randomImgName(n,formatimg="jpg"):
    return "img{}_{}.{}".format(str(time.time()).replace(".","")[:-4],n,formatimg)

path=os.path.join(".","dataset","clipRoad")
print(os.listdir(path))
# ['1-2有裂缝有树', 'DJI_0763', 'ModifyRoad.py', '1-1有裂缝无树', '1-2有裂缝有树标签', '1-1有裂缝无树标签', '0-1无裂缝无树', '0-2无裂缝有树']

hascrak=['1-2有裂缝有树', '1-1有裂缝无树']
hasnocrak=['0-1无裂缝无树', '0-2无裂缝有树']

# 创建一个新的图片文件，进行重新文件命名
if not os.path.exists(os.path.join(path,"images")):
    os.mkdir(os.path.join(path,"images"))
width, height=300,300
# 有裂缝
for dirname in hascrak:
    for imgname in os.listdir(os.path.join(path,dirname)):
        img=Image.open(os.path.join(path,dirname,imgname))
        img = img.resize((width, height), Image.ANTIALIAS)
        img.save(os.path.join(path,"images",randomImgName(1)))

# 无裂缝
for dirname in hasnocrak:
    for imgname in os.listdir(os.path.join(path,dirname)):
        img=Image.open(os.path.join(path,dirname,imgname))
        img = img.resize((width, height), Image.ANTIALIAS)
        img.save(os.path.join(path,"images",randomImgName(0)))